package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/14.<br><br>
 *
 * 有效的完全平方数
 */
public class Topic367 {
    public static void main(String[] args) {
        Topic367 instance = new Topic367();

        System.out.println(instance.isPerfectSquare(1));
        System.out.println(instance.isPerfectSquare(16));
        System.out.println(instance.isPerfectSquare(14));
        System.out.println(instance.isPerfectSquare(2147483647));
    }

    public boolean isPerfectSquare(int num) {
        int low = 1;
        int high = num;

        while (low <= high) {
            int mild = (low + high) / 2;
            long mul = (long) mild * mild;
            if(mul == num) {
                return true;
            }

            if(mul > num) {
                high = mild - 1;
            }

            if(mul < num) {
                low = mild + 1;
            }
        }

        return false;
    }

    public boolean isPerfectSquare2(int num) {
        int i = 1;
        while (true) {
            int mul = i * i;
            if(mul == num) {
                return true;
            }

            if(mul > num || mul < 0) {
                return false;
            }

            i++;
        }
    }
}
